package com.bhs.sansonglogistics.ui.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.EvaluationBean;
import com.bhs.sansonglogistics.dialog.VideoDialog;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

/**
 * 公司评论
 */
public class EvaluationAdapter extends BaseQuickAdapter<EvaluationBean.DataDTO.ListDTO, BaseViewHolder> {

    public EvaluationAdapter() {
        super(R.layout.item_evaluatio);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, EvaluationBean.DataDTO.ListDTO item) {
        if (item.getAnonymous().equals("0")) {
            Glide.with(mContext).load(item.getCargo_info().getHeaderpic()).into((ImageView) helper.getView(R.id.iv_head));
            helper.setText(R.id.tv_nick_name, item.getCargo_info().getNickname());
        } else {
            Glide.with(mContext).load(R.mipmap.ic_cow).into((ImageView) helper.getView(R.id.iv_head));
            helper.setText(R.id.tv_nick_name, "匿名用户");
        }
        DecimalFormat df = new DecimalFormat("#0.0");
        helper.setText(R.id.tv_score, String.format("%s分", df.format(Double.parseDouble(item.getAverage_score()))));
        helper.setText(R.id.tv_label, MyUtils.getOrderType2(item.getOrder_info().getOrder_type(), item.getOrder_info().getDeliver_type()));
        helper.setBackgroundRes(R.id.tv_label, item.getOrder_info().getOrder_type() == 1 ? R.drawable.bg_blue_4 : R.drawable.bg_yellow_4);
        helper.setText(R.id.tv_line, item.getOrder_info().getPickup_city() + "-" + item.getOrder_info().getReceive_city());
        helper.setText(R.id.tv_comment, item.getContent());
        helper.setText(R.id.tv_time, MyUtils.getBefore(item.getCreate_time()));
        RecyclerView mRvList = helper.getView(R.id.rv_list);
        Object[] imgData = item.getImgs().split("\\|\\|");
        if (imgData.length == 1) {
            //图片只有一张时显示一列
            mRvList.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        } else if (imgData.length == 2) {
            //图片两张或者四张时显示两列
            mRvList.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        } else if (imgData.length == 4) {
            //图片两张或者四张时显示两列
            mRvList.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL));
        } else {
            //其他数量图片显示三列
            mRvList.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        }
        List<Object> objectList = Arrays.asList(imgData);
        PictureShowAdapter2 mAdapter = new PictureShowAdapter2(objectList, imgData.length);
        mRvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                new XPopup.Builder(mContext)
                        .asImageViewer(view.findViewById(R.id.iv_picture), position, objectList, null, new SmartGlideImageLoader())
                        .show();
            }
        });
    }
}
